package com.scau.action;

import java.util.List;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.jasper.security.SecurityUtil;
import org.springframework.context.annotation.Scope;


import org.springframework.stereotype.Controller;

import com.scau.service.IAdminService;
import com.scau.vo.Admin;

/**
 * @author Jinglun
 * @create 2017年3月12日
 */
public class AdminAction extends SuperAction {
	private static final long serialVersionUID = 1L;
	public static Logger log = Logger.getLogger(AdminAction.class);
	private String result;// 属性result，用于异步传输数据交互

	private IAdminService adminService;
	private Admin admin;
	
	private String username;
	private String password;

	// 注册
	/*public String register() {
		Admin admin = getAdmin();
		System.out.println("注册用户为：" + admin);

		if (adminService.register(admin)) {
			session.setAttribute("curAdmin", admin);
			return "success";
		} else
			return "error";

	}*/
	
	// 查询所有的管理员
	public String listAdmin(){
		List<Admin> listAd = adminService.listAdmin();
		//System.out.println(listAd);
		JSONArray jsonArray = JSONArray.fromObject(listAd.toString());
		result = jsonArray.toString();
		return "success";
	}
	
	// 根据姓名查找管理员
	public String findByName(){
		Admin admin = getAdmin();
		String name = admin.getName();
		List<Admin> foundAdmin = adminService.findByName(name);
		JSONArray josnArray = JSONArray.fromObject(foundAdmin.toString());
		result = josnArray.toString();
		return "success";
	}
	
	// 登录的action
	public String login(){
		System.out.println("loginAction开始。");

		String username = admin.getName();
		String password = admin.getPassword();
		//System.out.println(username+password);
		//System.out.println("获取到的admin:" + admin);
		
		Subject subject = SecurityUtils.getSubject();
		String error = null;
		
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		token.setRememberMe(true);
		
		try {
			subject.login(token);
			System.out.println("用户：" + username + "登录成功！");
			return "login";
		} catch (UnknownAccountException e) {
			error = "用户名/密码错误";
			return "error";
		} catch (IncorrectCredentialsException e) {
			error = "用户名/密码错误";
			return "error";
		} catch (AuthenticationException e) {
			// 其他错误，比如锁定，如果想单独处理请单独 catch 处理
			error = "其他错误：" + e.getMessage();
			return "error";
		}
	
		//此方法不处理登陆成功，shiro认证成功会跳转到上一个路径 
	    //登陆失败，还到login页面
	    
	}

	
	
	// get,set方法
	public IAdminService getAdminService() {
		return adminService;
	}

	public void setAdminService(IAdminService adminService) {
		this.adminService = adminService;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	
}
